package com.dazhao.animejaphome.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.dazhao.animejaphome.R;
import com.dazhao.animejaphome.bean.SumBean;

import com.dazhao.animejaphome.httpservice.SumAPI;
import com.dazhao.animejaphome.home.NewsDetailsActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhao on 2018/2/28.
 */

public class NewsAdapter extends RecyclerView.Adapter{



        private int sum=0;
        Context context;
        ArrayList<String> titles;
//        ArrayList<String> texts;



        public NewsAdapter(Context context,ArrayList<String> titles){
            this.context=context;
            this.titles=titles;
//            texts=new ArrayList<>();
//            for(int i=0;i<10;i++){
//                titles.add("");
//                texts.add("");
//            }
            Retrofit retrofit= new Retrofit.Builder()
                    .baseUrl("http://114.67.131.62/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            SumAPI service = retrofit.create(SumAPI.class);
            Call<SumBean> sumBeanCall =service.repo("AnimeJap/sum.json");
            sumBeanCall.enqueue(new Callback<SumBean>() {
                @Override
                public void onResponse(Call<SumBean> call, Response<SumBean> response) {
                    sum=response.body().getSum();

                }

                @Override
                public void onFailure(Call<SumBean> call, Throwable t) {

                }
            });
        }


        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {//该函数应该是在position位置绑定数据
//将资源绑定到相关组件



            final TextView textView=((ViewHolder)holder).textView;
//            if(textView.getText().equals("")){
//                Retrofit retrofit= new Retrofit.Builder()
//                        .baseUrl("http://114.67.131.62/")
//                        .addConverterFactory(GsonConverterFactory.create())
//                        .build();
//                NewsTextAPI service = retrofit.create(NewsTextAPI.class);
//                Call<NewsTextBean> newsTextBeanCall =service.repo("AnimeJap/zx/"+intTo3(position+1)+".json");
////            Call<NewsTextBean> newsTextBeanCall =service.repo("AnimeJap/zx/001.json");
//                newsTextBeanCall.enqueue(new Callback<NewsTextBean>() {
//                    @Override
//                    public void onResponse(Call<NewsTextBean> call, final Response<NewsTextBean> response) {
//                        textView.setText(response.body().getTitle());
//                    }
//                    @Override
//                    public void onFailure(Call<NewsTextBean> call, Throwable t) {
//                    }
//                });
//
//            }

            textView.setText(titles.get(position));

            final ImageView imageView=((ViewHolder)holder).imageView;

            String Url = "http://114.67.131.62/AnimeJap/zx/"+intTo3(position+1)+".jpg";

            Picasso.with(context)
                    .load(Url)
                    .into(imageView);


            final Intent intent=new Intent(context,NewsDetailsActivity.class);

            intent.putExtra("url","http://114.67.131.62/AnimeJap/zx/"+intTo3(position+1)+".jpg");
            intent.putExtra("jsonUrl","AnimeJap/zx/"+intTo3(position+1)+".json");

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(intent);
                }
            });
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(intent);
                }
            });




        }

        @Override
        public int getItemCount() {
            //返回子项的总生成数
//        return mData == null ? 0 : mData.size();
//            return mData.size();


//这里的生成数目无效
            return 10;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//让RecyclerView知道子项长什么样oo
            //生成子项，并保持起来
            View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_news,parent,false);
            ViewHolder viewHolder=new ViewHolder(v);



//将创建的View注册点击事件


            return viewHolder;
        }




        public class ViewHolder extends RecyclerView.ViewHolder {

            ImageView imageView;
            TextView textView;



            public ViewHolder(View itemView) {
                //初始化子项的组件
                super(itemView);
                imageView = itemView.findViewById(R.id.image_view_news);
                textView = itemView.findViewById(R.id.text_news);


            }
        }

    //将整形转为3个字符
        private String intTo3(int i){
            String string="";
            if(i>=100){
                string=""+i;
            }else if(i>=10){
                string="0"+i;
            }else {
                string="00"+i;
            }
            return string;
        }

    }


